package com.example.mypro;
 

import com.example.domain.XueSheng;
import com.example.factory.ServiceFactory;
import com.example.mypro.R;
import com.example.service.XueShengService;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ZhuCeActivity extends Activity implements OnClickListener {
	private Button regist;
	private Button cancel;
	private Button change;
	private EditText user;
	private EditText password;
	private EditText telephone;
	private EditText classname;
	private EditText xueyuan;
	private EditText specialty;
	private EditText xuehao;
	private XueShengService xueShengService;
	private Boolean flag;
	private Boolean iszhuce;
	private GetData getData;
	private XueSheng xueSheng=new XueSheng();
	private int xh;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		xueShengService=ServiceFactory.getShengService();
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_zhuche);
		regist = (Button) findViewById(R.id.zhuce);
		change = (Button) findViewById(R.id.change);
		cancel = (Button) findViewById(R.id.quxiao);
		cancel.setOnClickListener(this);
		regist.setOnClickListener(this);
		change.setOnClickListener(this);
		user = (EditText) findViewById(R.id.et_user);
		password = (EditText) findViewById(R.id.et_password);
        classname=(EditText) findViewById(R.id.et_class);
        xuehao=(EditText) findViewById(R.id.et_bh);
        xueyuan=(EditText)findViewById(R.id.et_xueyuan);
        specialty = (EditText) findViewById(R.id.et_specialty);
        telephone = (EditText) findViewById(R.id.et_telephone2);
        
	}
	@SuppressLint("ShowToast")
	class GetData extends AsyncTask<Void, Void, Boolean> {

		@SuppressLint("ShowToast")
		@Override
		protected Boolean doInBackground(Void... params) {
			System.out.println("doInBackground");
	    xh=Integer.valueOf(xuehao.getText().toString());
        xueSheng.setXs_bj(classname.getText().toString());
        xueSheng.setXs_dh(telephone.getText().toString());
        xueSheng.setXs_mm(password.getText().toString());
        xueSheng.setXs_xh(xh);
        xueSheng.setXs_xm(user.getText().toString());
        xueSheng.setXs_zy(specialty.getText().toString());
        xueSheng.setXs_xy(xueyuan.getText().toString());
        if (iszhuce) {
			flag=xueShengService.save(xueSheng);
		} else {
			flag=xueShengService.update(xueSheng);
		}
        System.out.println(flag);
			return flag;
		}

		@Override
		protected void onProgressUpdate(Void... values) {
			super.onProgressUpdate(values);

		}

		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
                	Toast.makeText(ZhuCeActivity.this, "操作成功", Toast.LENGTH_LONG).show();
		}

	}
	@Override
	public void onClick(View v){
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.zhuce:
			iszhuce=true;
			getData = new GetData();
			getData.execute();
	        	this.finish();	
    		break;

		case R.id.quxiao:
			this.finish();
			break;
		case R.id.change:
			iszhuce=false;
			getData = new GetData();
			getData.execute();
		    this.finish();	
			break;

		}

	}

}
